;(function ($, global) { // 管理员登录
  let $form = $('#admin-login-form')
  let $sumbit = $form.find('.submit')
  let $captcha = $form.find('.captcha')
  let $captcha_img = $captcha.find('img.captcha-img')
  let req = null
  let msg = ''

  let verify = {
    signname: {
      required: true,
      errMsg: '用户名不能为空'
    },
    password: {
      required: true,
      errMsg: '密码不能为空'
    }
  }

  let trigger = function () {
    $sumbit.click(function () {
      req = initReq()
      if (reqVerify(req, verify)) {
        req.password = md5(req.password)
        _ajax(req, suc, err)
      }
    })
  }

  let initReq = function () {
    return {
      _token: $form.find('input[name=_token]').val(),
      signname: $form.find('input[name=signname]').val(),
      password: $form.find('input[name=password]').val(),
      captcha: $form.find('input[name=captcha]').val()
    }
  }

  let _ajax = function (data, success, error) {api_ajax('POST', 'doLogin', data, 'json', success, error)}

  let suc = function (data) {
    layer.msg("登录成功")
    localStorage.setItem('admin_aid', data.aid)
    localStorage.setItem('admin_signname', data.signname)
    localStorage.setItem('admin_nickname', data.nickname)
    localStorage.setItem('admin_gid', data.gid)
    if (data.redirect) {
      window.location.href = data.redirect
    }
  }

  let err = function (data) {
    if (data.login_need_captcha == 1) {
      $captcha.show()

      if (data.captcha_src) {
        $captcha_img.attr('src', data.captcha_src)
      }
    }else {
      $captcha.hide()
    }
    console.log(data)
  }

  trigger()
}(jQuery, window))
